package com.part2.action.function;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.part2.action.common.Config;
import com.part2.action.common.form.CustomActionForm;
import com.part2.action.common.form.ValueObject;
import com.part2.action.common.validator.Validator;

public class CompanyConfigAction extends DispatchAction {
	private final Log logger = LogFactory.getLog(getClass());

	public ActionForward preConfig(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ValueObject cVo = ((CustomActionForm) form).getVoa().getValue("config");
		//CN
		cVo.setField("webName", Config.readValue("webName"));
		cVo.setField("webSite", Config.readValue("webSite"));
		cVo.setField("companyName", Config.readValue("companyName"));
		cVo.setField("address", Config.readValue("address"));
		cVo.setField("COPYRIGHT", Config.readValue("COPYRIGHT"));
		cVo.setField("salephone", Config.readValue("Salephone"));
		cVo.setField("complainPhone", Config.readValue("complainPhone"));
		cVo.setField("fax", Config.readValue("fax"));
		cVo.setField("introduce", Config.readValue("Introduce"));
		cVo.setField("culture", Config.readValue("culture"));
		cVo.setField("structure", Config.readValue("structure"));
		cVo.setField("contanct", Config.readValue("Contanct"));
		
		//EN
		cVo.setField("webNameEn", Config.readValue("webNameEn"));
		cVo.setField("companyNameEn", Config.readValue("companyNameEn"));
		cVo.setField("addressEn", Config.readValue("addressEn"));
		cVo.setField("introduceEn", Config.readValue("introduceEn"));
		cVo.setField("cultureEn", Config.readValue("cultureEn"));
		cVo.setField("structureEn", Config.readValue("structureEn"));
		cVo.setField("contanctEn", Config.readValue("contanctEn"));
		
		return mapping.findForward("config");
	}

	public ActionForward config(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("::::::::::Config");
		ValueObject cVo = ((CustomActionForm) form).getVoa().getValue("config");
		//CN
		Config.writeProperties("webName", cVo.getField("webName"));
		Config.writeProperties("webSite", cVo.getField("webSite"));
		Config.writeProperties("companyName", cVo.getField("companyName"));
		Config.writeProperties("address", cVo.getField("address"));
		Config.writeProperties("COPYRIGHT", cVo.getField("COPYRIGHT"));
		Config.writeProperties("Salephone", cVo.getField("salePhone"));
		Config.writeProperties("complainPhone", cVo.getField("complainPhone"));
		Config.writeProperties("fax", cVo.getField("fax"));
		Config.writeProperties("Introduce", cVo.getField("introduce"));
		Config.writeProperties("culture", cVo.getField("culture"));
		Config.writeProperties("structure", cVo.getField("structure"));
		Config.writeProperties("Contanct", cVo.getField("contanct"));
		
		//EN
		Config.writeProperties("webNameEn", cVo.getField("webNameEn"));
		Config.writeProperties("companyNameEn", cVo.getField("companyNameEn"));
		Config.writeProperties("addressEn", cVo.getField("addressEn"));
		Config.writeProperties("IntroduceEn", cVo.getField("introduceEn"));
		Config.writeProperties("cultureEn", cVo.getField("cultureEn"));
		Config.writeProperties("structureEn", cVo.getField("structureEn"));
		Config.writeProperties("ContanctEn", cVo.getField("contanctEn"));

		logger.info("::::::::::Config End");
		return preConfig(mapping, form, request, response);
	}
	public ActionForward about(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String companyInfo = Config.readValue("Introduce");
		if(Validator.isNotNull(companyInfo))
			request.setAttribute("companyInfo", companyInfo);
		return mapping.findForward("about");
	}
	public ActionForward connect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String connect = Config.readValue("Contanct");
		if(Validator.isNotNull(connect))
			request.setAttribute("connect", connect);
		return mapping.findForward("connect");
	}
	public ActionForward struct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String struct = Config.readValue("structure");
		if(Validator.isNotNull(struct))
			request.setAttribute("struct", struct);
		return mapping.findForward("struct");
	}
	public ActionForward culture(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String culture = Config.readValue("culture");
		if(Validator.isNotNull(culture))
			request.setAttribute("culture", culture);
		return mapping.findForward("culture");
	}
}
